from manim import *
class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-40, 5, 0],
            [-30,-5,0],
            [-40,-5,0],
            [-40,5,0],
            
        ]
        #self.set_points_smoothly(points)#丝滑曲线
        self.set_points_as_corners(points)#直线
        
class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{设平面区域}$D=\{  \left( x,y\right) | x+y\leq 1,x\geq 0,y\geq  0\} $\text{,计算}$\iint _{D}\dfrac{e^{-\left( x+y\right) }}{\sqrt{xy}}d\sigma $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0)
       
      
        self.play(Write(text),self.camera.frame.animate.scale(0.8))
        
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([0,20,0]))

        # 创建坐标系
        axes = Axes(
            x_range=[-3, 10, 3],  # 设置 x 轴范围，从 -π 到 π，步长为 π
            y_range=[-3, 10, 3],  # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE, "stroke_width": 30},  # 设置坐标轴的颜色和线宽
            x_length=20,  # 控制 x 轴长度
            y_length=20,  # 控制 y 轴长度
        )
        axes.move_to([-35, 0, 0])  # 移动坐标系的位置

        # 绘制 1/x 函数图像
        # 分为两部分以避开 x=0
        #graph_left = axes.plot(lambda x: 5/x, color=BLUE, stroke_width=30, x_range=[-5, -0.1])
        #graph_right = axes.plot(lambda x: 5/x, color=PURE_RED, stroke_width=30, x_range=[0.1, 10])
        
       

        
       


        #self.play(Create(arrow10),Create(text15))
        # 将所有元素添加到场景中
        #self.play(Create(axes), Create(labels), Create(graph2))
         # 显示图像
        
        self.play(Create(axes))
       # 添加到场景中
        curve = CustomCurve(color=RED, stroke_width=20)
        
        # 使用 set_fill 来填充颜色
        curve.set_fill(color=GREEN, opacity=0.5)  # 绿色填充，50%透明度
        self.play(Create(curve))

        dot1=Dot([-45,-10,0])
        dot2=Dot([-25,10,0])
        line=Line(dot1,dot2).set_color(PURE_GREEN)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))

        text112 = Tex(r"\text{关于}$y=x$\text{对称}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-15,0])
        self.play(Create(text112))
        text113 = Tex(r"\text{(轮换对称性)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text112,RIGHT,buff=1)

        

        self.play(Create(text113))
        
         #箭头引出
        arrow = Arrow(start=[15,25,0],end=[20,23,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本
        long_text=("特征识别:\n"
                     "被积函数具有轮换性")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([0,25,0])
        #框框标
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=8,height=7
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([31, 20, 0])
        self.play(Create(rectangle),Create(text100))

        text200 = Tex(r"\text{轮换性效果抵消}$!!$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([0,0,0])

        

        self.play(Create(text200))
        self.play(FadeOut(text200))



        text10 = Tex(r"$\int _{0}^{1}dx\int _{0}^{x}\dfrac{e^{-\left( x+y\right) }}{\sqrt{xy}}d\sigma $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22.5,12,0])
        self.play(Create(text10))
        text100 = Tex(r"\text{(先x后y)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text10,RIGHT,buff=1)
        self.play(Create(text100))


        text11 = Tex(r"$=\int ^{1}_{0}\dfrac{e^{-x}}{\sqrt{x}}dx\int _{0}^{x}\dfrac{e^{-y}}{\sqrt{y}}dy$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22.5,0,0])
        self.play(Create(text11))

        #框框标
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=11,height=10
                            ,stroke_color=RED,stroke_width=20)
        rectangle1.move_to([29.5, 0, 0])
        self.play(Create(rectangle1))

        self.play(Uncreate(text11),Uncreate(text10),Uncreate(text100),Uncreate(rectangle1))

        #箭头引出
        arrow = Arrow(start=[-20,7,0],end=[-25,5,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本
        long_text=("特征识别:\n"
                     "区域为角形区域")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([-8,10,0])
        self.play(Create(text100))

        text3 = Tex(r"\text{(极坐标)}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text100,DOWN,buff=1)
        self.play(Create(text3))
        
        text10 = Tex(r"$\int _{0}^{\dfrac{\pi }{2}}d\theta \int _{0}^{\dfrac{1}{\sin \theta +\cos \theta }}\dfrac{e^{-r\left( \cos \theta +\sin \theta \right) }}{\sqrt{\sin \theta \cos \theta }}dr$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([23.5,12,0])
        self.play(Create(text10))

        text100 = Tex(r"$x+y\rightarrow r\left( \sin \theta +\cos \theta \right) $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22.5,5,0])
        self.play(Create(text100))
        text1000 = Tex(r"\text{加减}$\rightarrow $\text{乘积}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22.5,1,0])
        self.play(Create(text1000))


        text11 = Tex(r"$\int _{0}^{\dfrac{\pi }{2}}\dfrac{1}{\sqrt{\sin \theta \cos \theta }}d\theta \int ^{\dfrac{1}{\sin \theta +\cos \theta }}_{0}\dfrac{}{e}r\left( \cos \theta +sin\theta \right) _{dr}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([17.5,-5,0])
        self.play(Create(text11))
        dot3=Dot([10,-10,0])
        dot4=Dot([47,-10,0])
        line=Line(dot3,dot4).set_color(PURE_RED)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        combined = Group(text112, text113)
        self.play(combined.animate().scale(1).shift(DOWN*10)) 
        text12 = Tex(r"$\int _{0}^{\dfrac{\pi }{2}}\dfrac{1}{\sqrt{\sin \theta \cos \theta }}\cdot \dfrac{1}{\sin \theta +\cos \theta }d\theta $",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-15,0])
        text13 = Tex(r"$ \int _{0}^{\dfrac{1}{\sin \theta +\cos \theta }}e^{-r\left( \sin \theta +\cos \theta \right) }dr\left( \sin \theta +\cos \theta \right) $",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text12,RIGHT,buff=1)
        self.play(Create(text12))
        self.play(Create(text13))

        dot3=Dot([-5,-20,0])
        dot4=Dot([47,-20,0])
        line=Line(dot3,dot4).set_color(PURE_BLUE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
       
        text14 = Tex(r"$ \left( 1-\dfrac{1}{e}\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-25,0])
        
        self.play(Create(text14),FadeOut(text113),FadeOut(text112))

        self.play(FadeOut(text13),FadeOut(line),text12.animate().scale(1).shift(RIGHT*40))
        self.play(text14.animate().scale(1).next_to(text12,LEFT,buff=1))

         #箭头引出
        arrow = Arrow(start=[-7,-23,0],end=[0,-20,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本
        long_text=("特征识别:\n"
                     "伪奇偶性")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([-15,-23,0])
        self.play(Create(text100))
        text16 = Tex(r"\text{分子分母同除}$\cos ^{2}x $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-23,0])
        self.play(Create(text16))
        text15 = Tex(r"$\int _{0}^{\dfrac{\pi }{2}}\dfrac{1}{\sqrt{\tan }\overline{\theta }\left( 1+\tan \theta \right) }d\tan \theta $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text12,text15)
            
        )
        self.wait(0.2)

        text17 = Tex(r"\text{令}$\sqrt{\tan \theta }=u$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-23,0])
        self.play(
            ReplacementTransform(text16,text17)
            
        )
        self.wait(0.2)

        text18 = Tex(r"$\int _{0}^{+\infty }\dfrac{2}{1+u^{2}}du$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text15,text18)
            
        )
        self.wait(0.2)

        text19 = Tex(r"$ 2\arctan u| _{0}^{+\infty }$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text18,text19)
            
        )
        self.wait(0.2)

        text20 = Tex(r"$\pi \left( 1-\dfrac{1}{e}\right) $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-15,0])
        self.play(
            ReplacementTransform(text19,text20),
            FadeOut(text17),
            ReplacementTransform(text14,text20),
            
        )
        self.wait(0.2)

        self.wait()
      